package com.course.business.controller.admin;

import com.course.server.domain.Section;
import com.course.server.dto.SectionDTO;
import com.course.server.dto.PageDTO;
import com.course.server.dto.ResponseDTO;
import com.course.server.service.SectionService;
import com.course.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/section")
public class SectionController {

    private static final Logger logger = LoggerFactory.getLogger(SectionController.class);

    public static final String BUSINESS_NAME = "小节";

    @Resource
    private SectionService sectionService;

    @PostMapping("/section")
    public String section(){
        return "success";
    }

    @PostMapping("/list")
    public ResponseDTO list(@RequestBody PageDTO pageDTO){
        logger.info("pageDTO:{}", pageDTO);
        ResponseDTO responseDTO = new ResponseDTO();
        sectionService.list(pageDTO);
        responseDTO.setContent(pageDTO);
        return responseDTO;
    }

    @PostMapping("/save")
    public ResponseDTO save(@RequestBody SectionDTO sectionDTO){
        logger.info("sectionDTO:{}", sectionDTO);

        //保存校验
        ValidatorUtil.require(sectionDTO.getTitle(), "标题");
        ValidatorUtil.length(sectionDTO.getTitle(), "标题", 1, 50);
        ValidatorUtil.length(sectionDTO.getVideo(), "视频", 1, 200);


        ResponseDTO responseDTO = new ResponseDTO<>();
        sectionService.save(sectionDTO);
        responseDTO.setContent(sectionDTO);
        return responseDTO;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDTO save(@PathVariable String id){
        logger.info("id:{}", id);
        ResponseDTO responseDTO = new ResponseDTO<>();
        sectionService.delete(id);
        return responseDTO;
    }
}
